<template>
	<view class="content">
		<view class="navbar">
			<view 
				v-for="(item, index) in navList" :key="index" 
				class="nav-item" 
				:class="{current: tabCurrentIndex === index, current1: index === 0, current2: index === navList.length - 1}"
				@click="tabClick(index)"
			>
				{{item.text}}
			</view>
		</view>
		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem,tabIndex) in navList" :key="tabIndex">
				<scroll-view 
					class="list-scroll-content" 
					scroll-y
					@scrolltolower="loadData('dropDown')"
				>
					<!-- 空白页 -->
					<empty v-if="tabItem.orderList.length === 0" title="您还没有相关的订单" text="可以去看看有哪些想买的"></empty>
					<!-- 订单列表 -->
					<view 
						v-for="(item,index) in tabItem.orderList" :key="index"
						class="order-item"
						@click="toOrder(item)"
						v-if="tabItem.orderList.length > 0"
					>
						<view class="i-top b-b">
							<text class="time">{{item.addDateTime}}</text>
							<!-- 待支付的订单 显示支付状态-->
							<text class="state" v-if="item.orderStatus=== '0'" style="color: 999;">{{pay_status[item.payStatus]}}</text>
							<!-- 待发货的订单 显示配送状态-->
							<text class="state" v-if="item.orderStatus==='1'" style="color: 999;">{{shipping_status[item.shippingStatus]}}</text>
							<!-- 待收货的订单 待收货-->
							<text class="state" v-if="item.orderStatus==='2'" style="color: 999;">待收货</text> 
							<!-- 待评价的订单 待评价-->
							<text class="state" v-if="item.orderStatus==='3'" style="color: 999;">待评价</text>
							<!-- 已完成的订单 已完成-->
							<text class="state" v-if="item.orderStatus==='4'" style="color: 999;">已完成</text> 
							<!-- 退款售后的订单 待评价-->
							<text class="state" v-if="item.orderStatus==='5'" style="color: 999;">售后中</text> 
							<text class="state" v-if="item.orderStatus==='6'" style="color: 999;">退款完成</text>
							<text class="state" v-if="item.orderStatus==='7'" style="color: 999;">退货完成</text> 
							<text class="state" v-if="item.orderStatus==='8'" style="color: 999;">换货完成</text> 
							<text 
								v-if="item.orderStatus===9" 
								class="del-btn yticon icon-iconfontshanchu1"
								@click="deleteOrder(index)"
							></text>
						</view>
						<view class="goods-info">
							<view class="img">
								<image  :src="item.originalImg | formatUrl" @tap.stop="showImg(item.originalImg)" mode="aspectFill"></image>
							</view>
							<view class="info">
								<view class="title">{{item.goodsName}}</view>
								<view class="spec">{{item.goodsAttr}} </view>
							</view>
							<view class="price-number">
								<view class="price">￥{{item.goodsPrice|toFixed}}</view>
								<view class="number" style="text-align: right;"> x {{item.goodsCount}}</view>
							</view>
						</view>
						<view class="order_info_tip">
							<span class="order_info_tip1">共{{item.goodsCount}}件： </span>
							<span class="order_info_tip2">￥{{item.orderAmount |toFixed}}</span>
						</view>
						<view class="action-box b-t" v-if="item.orderStatus != 99 ">
							<!-- 待支付的订单 -->
							<button class="action-btn" v-if="item.orderStatus === '0' " @click.stop="deleteOrder(index, item)">取消订单</button>
							<button class="action-btn recom" v-if="item.orderStatus === '0'" @click.stop="pay(item)">{{item.payStatus === '0'? '立即支付': '重新支付'}}</button>
							<!-- 待发货的订单 无按钮-->
							<!-- 待收获的订单 -->
							<button class="action-btn recom" v-if="item.orderStatus === '2'" @click.stop="configReceipt(item)">确认收货</button>
							<!-- 待评价的订单 -->
							<button class="action-btn recom" v-if="item.orderStatus === '3'" @click.stop="evaluate(item)">评价</button>
							<!-- 已完成的订单  查看评价-->
							<button class="action-btn recom" v-if="item.orderStatus === '4'" @click.stop="comment(item)">查看评价</button>
							<!-- 退款售后的订单 无按钮-->
						</view>
					</view>
					 <view
						v-if="tabItem.orderList.length === 0"
					 >		 
					 </view>
				</scroll-view>
			</swiper-item>
		</swiper>
		<popup-layer ref="popupRef" :direction="'top'" v-model="boolShow">
			<view class="replacement_box" >
				<view class="replacement_box_title">取消理由</view>
				 <scroll-view 
					scroll-top="0"
					scroll-y="true"
					style="height: 100%;"
				>
					<view class="replacement_box_item" v-for="(item, index) in reasons" :key="index">
							<view class="replacement_box_text">{{item.title}}</view>
						<!-- checkbox --> 
						<view class="checkbox-box" @tap="selected(item.value)">
							<view class="checkbox">
								<view :class="[reason === item.value ? 'on' : '']"></view>
							</view>
						</view>
					</view>
					<view class="replacement_box_item" style="margin: 20px">
						<view class="replacement_box_text">取消理由</view>
						<view class="replacement_box_right">
							<input placeholder="请留下您宝贵的建议" v-model="CancelReasonStr"/>
						</view>
					</view>
				</scroll-view>
			    <button type="primary" class="feedback-submit replacement_button" @tap="confirmDelete(item)" >完成</button>
			</view>
		</popup-layer>			
		<uni-fab ref="fab" :pattern="pattern" :content="content" :horizontal="horizontal" :vertical="vertical" :direction="direction" @trigger="trigger" />
	</view>
</template> 

<script>
	import uniFab from '@/components/uni-fab/uni-fab.vue'
	import empty from "@/components/empty";
	import Json from '@/Json';
	import { mapActions } from 'vuex';
	import orderApis from '@/common/services/order.js';
	import otherApis from '@/common/services/other.js';
	import popupLayer from '@/components/popup-layer/popup-layer.vue';
	import { getQuery } from '@/utils/utils.js'
	export default {
		components: {
			empty,
			popupLayer,
			uniFab
		},
		data() {
			return {
				horizontal: 'right',
				vertical: 'bottom',
				direction: 'horizontal',
				pattern: {
					color: '#7A7E83',
					backgroundColor: '#fff',
					selectedColor: '#A2313E',
					buttonColor: 'rgb(0,0,0)'
				},
				content: [
					{
						iconPath: "/static/icon/chat.png",
						selectedIconPath: "/static/icon/chat1.png",
						text: '联系客服',
						active: false
					},
					{
						iconPath: "/static/icon/home.png",
						selectedIconPath: "/static/icon/home1.png",
						text: '首页',
						active: false
					},
					{
						iconPath: "/static/icon/type.png",
						selectedIconPath: "/static/icon/type1.png",
						text: '分类',
						active: false
					},
					{
						iconPath: "/static/icon/shopCart.png",
						selectedIconPath: "/static/icon/shopCart1.png",
						text: '购物车',
						active: false
					}
				],
				reasons: [], // 取消订单的理由
				reason: '', // 用户选择的取消订单理由
				CancelReasonStr: '', // 用户填写的取消理由
				boolShow:false,
				tabCurrentIndex: 0,
				orderCurrentIndex: 0,
				pay_status: ['未付款','付款中','已付款', '付款失败', '退款中', '已退款'], // 支付状态
				shipping_status: ['未发货', '备货中', '出库中', '已发货', '运输中', '投递中', '确认收货'], // 配送状态
				navList: [{
						state: 0,
						text: '全部',
						loadingType: 'noMore',
						orderList: [],
						orderType: 999,
						pageNo: 0,
						pageSize: 10,
					},
					{
						state: 1,
						text: '待支付',
						loadingType: 'noMore',
						orderList: [],
						orderType: 0,
						pageNo: 0,
						pageSize: 10,
					},
					{
						state: 2,
						text: '待发货',
						loadingType: 'noMore',
						orderList: [],
						orderType: 1,
						pageNo: 0,
						pageSize: 10,
					},
					{
						state: 3,
						text: '待收货',
						loadingType: 'noMore',
						orderList: [],
						orderType: 2,
						pageNo: 0,
						pageSize: 10,
					},
					{
						state: 4,
						text: '待评价',
						loadingType: 'noMore',
						orderList: [],
						orderType: 3,
						pageNo: 0,
						pageSize: 10,
					},
					{
						state: 5,
						text: '退款售后',
						loadingType: 'noMore',
						orderList: [],
						orderType: 4,
						pageNo: 0,
						pageSize: 10,
					}
				],
			};
		},
		filters: {
			toFixed:function(x) {
				return parseFloat(x).toFixed(2);
			},
			formatUrl: function(url) {
				return url && url.indexOf('http') !== -1 ? url : 'http://106.13.76.109:8080/xsmall/sys/common/view/'+ url
			}
		},
		// 监听用户下拉刷新
		async onPullDownRefresh() {
			await this.loadData('tabChange')
			uni.stopPullDownRefresh();
		},
		onLoad(options){
			otherApis.getUnicode('cancel_reason').then(res => {
				this.reasons = res;
			})
			uni.showTabBar()
		},
		onShow() {
			this.tabCurrentIndex = +getQuery('state') || 0;
			this.loadData('onShow');
		},
		methods: {
			// 图片预览的功能
			showImg(url) {
				const src = url.indexOf('http') !== -1 ? url : 'http://106.13.76.109:8080/xsmall/sys/common/view/'+ url;
				uni.previewImage({
					current: src,
					urls: [src]
				});
			},
			//获取订单列表s
			async loadData(source = 'tabChange'){ // 加载数据，一是下拉加载 'dropDown'， 二是tab切换 'tabChange'， 三是'onShow' 第一次进入页面
				//这里是将订单挂载到tab列表下
				let index = source === 'onShow' ? (+getQuery('state') || 0) : this.tabCurrentIndex;
				let navItem = this.navList[index];
				let pageNo = navItem.pageNo;
				// tab切换也需要重新加载数据， 因为订单的状态一直在修改
				if(navItem.loadingType === 'loading'){
					//防止重复加载
					return;
				}
				if(source === 'dropDown'){
					// 下拉刷新需要进行翻页
					pageNo = pageNo + 1;
				}
				navItem.loadingType = 'loading';
				await orderApis.getOrderList({orderType: navItem.orderType, pageNo, pageSize: navItem.pageSize}).then(res => {
					if (source === 'dropDown') {
						res.forEach(item=>{
							navItem.orderList.push(item);
						})
					} else {
						navItem.orderList = res;
					}
					this.$set(navItem, 'loaded', true);
					navItem.loadingType = res.length === 10 ? 'more' : 'noMore';
				})
			}, 

			//swiper 切换
			changeTab(e){
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			//顶部tab点击
			tabClick(index){
				this.tabCurrentIndex = index;
			},
			//取消订单
			deleteOrder(index){
				this.orderCurrentIndex = index;
				// 需要弹出取消订单的理由 点击确认之后才能取消
				this.$refs.popupRef.show();
			},
			// 确认取消
			confirmDelete(){
				let navItem = this.navList[this.tabCurrentIndex];
				let orderList = navItem.orderList
				const item = orderList && orderList[this.orderCurrentIndex];
				orderApis.cancelOrder(
					{orderId: item.orderId, CancelReasonStr: this.CancelReasonStr + this.reasons[this.reason].title},
				).then(res => {	
					this.$refs.popupRef.close();
				})
				setTimeout((index)=>{
					this.navList[this.tabCurrentIndex].orderList.splice(this.orderCurrentIndex, 1);
				}, 300)
			},
			selected(value) {
				this.reason = value;
			},
			// 退货退款审核
			checkoutOrder(id) {
				uni.navigateTo({
					url: `../replacement/replacement?orderId=${id}`
				})
			},
			// 支付订单
			pay(item) {
				const sumPrice = item.orderAmount;
				uni.navigateTo({
					url: `../pay/payment/payment?amount=${sumPrice}&orderId=${item.orderId}`
				})
			},
			// 确认收货
			configReceipt(item) {
				orderApis.confirmReceive(item.orderId).then(res => {
					uni.showToast({
						icon: 'none',
						title: '收货成功',
					});	
					this.loadData('tabChange');
				})
			},
			// 评价购物
			evaluate(item) {
				this.setOrderInfo({ orderInfo: [item]})
				uni.navigateTo({
					url: `../publish/publish`
				})
			},
			// 查看评价
			comment(item) {
				uni.navigateTo({
					url: '../comment/comment'
				})
			},
			// 联系客服
			toChat(item) {
				uni.navigateTo({
					url: '../chat/chat'
				})
			},
			// 查看订单详情
			toOrder(item) {
				this.setOrderInfo({ orderInfo: [
					{
						...item,
						goodsNumber: item.goodsCount,
						shopPrice: item.goodsPrice,
						insure: item.shippingFee
					},
				]})
				uni.navigateTo({
					url: `../order/confirmation?orderId=${item.orderId}`
				})
			},
			trigger(e) {
				switch (e.index){
					case 0:
						uni.navigateTo({
							url: '../chat/chat',
						})
						break;
					case 1:
						uni.reLaunch({
							url: '../home/index',
							animationType: 'pop-in',
							animationDuration: 200
						});
						break;
					case 2:
						uni.reLaunch({
							url: '../class/index',
						});
						break;
					case 3:
						uni.reLaunch({
							url: '../shopCart/shopCart',
						});
						break;
					default:
						break;
				}
			},
			...mapActions({
				setOrderInfo: 'SET_ORDER_INFO'
			})
		},
					/**
		 *  点击导航栏 buttons 时触发
		 */
		onNavigationBarButtonTap() {
			uni.showModal({
				title: '提示',
				content: '用户点击了聊天按钮，这里正在开发。',
				success: res => {
					if (res.confirm) {
						console.log('用户点击了确定');
					}
				}
			});
		},
	}
</script>

<style lang="scss">
	page, .content{
		background: $page-color-base;
		height: 100%;
	}
	
	.swiper-box{
		height: calc(100% - 40px);
	}
	.list-scroll-content{
		height: 100%;
	}
	
	.navbar{
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0,0,0,.06);
		position: relative;
		z-index: 10;
		.nav-item{
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: $font-color-dark;
			position: relative;
			&.current{
				color: $base-color;
				&:after{
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
			&.current1 {
				flex: 0.67;
			}
			&.current2 {
				flex: 1.33;
			}
		}
	}

	.uni-swiper-item{
		height: auto;
	}
	.order-item{
		display: flex;
		flex-direction: column;
		padding: 20upx;
		background: #fff;
		margin-top: 16upx;
		.goods-info{
			width: 100%;
			display: flex;
			.img{
				width: 22vw;
				height: 22vw;
				border-radius: 10upx;
				overflow: hidden;
				flex-shrink: 0;
				margin-right: 10upx;
				image{
					width: 22vw;
					height: 22vw;
				}
			}
			.info{
				width: 100%;
				position: relative;
				.title{
					width: 100%;
					font-size: 28upx;
				}
				.spec{
					font-size: 24upx;
					color: #666;
					display: flex;
					align-items: center;
				}
			}
			.price-number{
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: flex-end;
				font-size: 28upx;
				height: 40upx;
				.price{
					color: #333;
				}
				.number{
					display: flex;
					justify-content: center;
					align-items: center;
					color: #888;
				}
			}
		}
		.order_info_tip {
			display: flex;
			justify-content: flex-end;
			margin: 20upx 0;
			.order_info_tip1 {
				color: #a7a7a7;
			}
			.order_info_tip2 {
				color: #E80505;
			}
		}
		.i-top{
			display: flex;
			align-items: center;
			height: 80upx;
			font-size: $font-base;
			color: $font-color-dark;
			position: relative;
			.time{
				flex: 1;
			}
			.state{
				color: $base-color;
			}
			.del-btn{
				padding: 10upx 0 10upx 36upx;
				font-size: $font-lg;
				color: $font-color-light;
				position: relative;
				&:after{
					content: '';
					width: 0;
					height: 30upx;
					border-left: 1px solid $border-color-dark;
					position: absolute;
					left: 20upx;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}
		/* 多条商品 */
		.goods-box{
			height: 160upx;
			padding: 20upx 0;
			white-space: nowrap;
			.goods-item{
				width: 120upx;
				height: 120upx;
				display: inline-block;
				margin-right: 24upx;
			}
			.goods-img{
				display: block;
				width: 100%;
				height: 100%;
			}
		}
		/* 单条商品 */
		.goods-box-single{
			display: flex;
			padding: 20upx 0;
			.goods-img{
				display: block;
				width: 120upx;
				height: 120upx;
			}
			.right{
				flex: 1;
				display: flex;
				flex-direction: column;
				padding: 0 30upx 0 24upx;
				overflow: hidden;
				.title{
					font-size: $font-base + 2upx;
					color: $font-color-dark;
					line-height: 1;
				}
				.attr-box{
					font-size: $font-sm + 2upx;
					color: $font-color-light;
					padding: 10upx 12upx;
				}
				.price{
					font-size: $font-base + 2upx;
					color: $font-color-dark;
					&:before{
						content: '￥';
						font-size: $font-sm;
						margin: 0 2upx 0 8upx;
					}
				}
			}
		}
		
		.price-box{
			display: flex;
			justify-content: flex-end;
			align-items: baseline;
			padding: 20upx 30upx;
			font-size: $font-sm + 2upx;
			color: $font-color-light;
			.num{
				margin: 0 8upx;
				color: $font-color-dark;
			}
			.price{
				font-size: $font-lg;
				color: $font-color-dark;
				&:before{
					content: '￥';
					font-size: $font-sm;
					margin: 0 2upx 0 8upx;
				}
			}
		}
		.action-box{
			display: flex;
			justify-content: flex-end;
			align-items: center;
			height: 100upx;
			position: relative;
			padding-right: 30upx;
		}
		.action-btn{
			width: 160upx;
			height: 60upx;
			margin: 0;
			margin-left: 24upx;
			padding: 0;
			text-align: center;
			line-height: 60upx;
			font-size: $font-sm + 2upx;
			color: $font-color-dark;
			background: #fff;
			border-radius: 100px;
			&:after{
				border-radius: 100px;
			}
			&.recom{
				background: #fff9f9;
				color: $base-color;
				&:after{
					border-color: #f7bcc8;
				}
			}
		}
	}
	
	
	/* load-more */
	.uni-load-more {
		display: flex;
		flex-direction: row;
		height: 80upx;
		align-items: center;
		justify-content: center
	}
	
	.uni-load-more__text {
		font-size: 28upx;
		color: #999
	}
	
	.uni-load-more__img {
		height: 24px;
		width: 24px;
		margin-right: 10px
	}
	
	.uni-load-more__img>view {
		position: absolute
	}
	
	.uni-load-more__img>view view {
		width: 6px;
		height: 2px;
		border-top-left-radius: 1px;
		border-bottom-left-radius: 1px;
		background: #999;
		position: absolute;
		opacity: .2;
		transform-origin: 50%;
		animation: load 1.56s ease infinite
	}
	
	.uni-load-more__img>view view:nth-child(1) {
		transform: rotate(90deg);
		top: 2px;
		left: 9px
	}
	
	.uni-load-more__img>view view:nth-child(2) {
		transform: rotate(180deg);
		top: 11px;
		right: 0
	}
	
	.uni-load-more__img>view view:nth-child(3) {
		transform: rotate(270deg);
		bottom: 2px;
		left: 9px
	}
	
	.uni-load-more__img>view view:nth-child(4) {
		top: 11px;
		left: 0
	}
	
	.load1,
	.load2,
	.load3 {
		height: 24px;
		width: 24px
	}
	
	.load2 {
		transform: rotate(30deg)
	}
	
	.load3 {
		transform: rotate(60deg)
	}
	
	.load1 view:nth-child(1) {
		animation-delay: 0s
	}
	
	.load2 view:nth-child(1) {
		animation-delay: .13s
	}
	
	.load3 view:nth-child(1) {
		animation-delay: .26s
	}
	
	.load1 view:nth-child(2) {
		animation-delay: .39s
	}
	
	.load2 view:nth-child(2) {
		animation-delay: .52s
	}
	
	.load3 view:nth-child(2) {
		animation-delay: .65s
	}
	
	.load1 view:nth-child(3) {
		animation-delay: .78s
	}
	
	.load2 view:nth-child(3) {
		animation-delay: .91s
	}
	
	.load3 view:nth-child(3) {
		animation-delay: 1.04s
	}
	
	.load1 view:nth-child(4) {
		animation-delay: 1.17s
	}
	
	.load2 view:nth-child(4) {
		animation-delay: 1.3s
	}
	
	.load3 view:nth-child(4) {
		animation-delay: 1.43s
	}
	
	@-webkit-keyframes load {
		0% {
			opacity: 1
		}
	
		100% {
			opacity: .2
		}
	}
	.replacement_box {
		width: 100%;
		height: 800upx;
		display: flex;
		flex-direction: column;
		padding: 0upx 20upx 180upx;;
		box-sizing: border-box;
		.feedback-submit {
			box-shadow: 0upx 5upx 10upx rgba(0,0,0,0.4);
			margin: 20upx auto;
			height: 80upx;
			background-color: #E80505;
			color: #fff;
			justify-content: center;
			align-items: center;
			&:hover {
				background-color: #862732;
			}
		 }
		.replacement_button {
			position: fixed;
			bottom: 0;
			width: 95%
		}
		.replacement_box_title {
			display: flex;
			align-items: center;
			font-size: 30upx;
			text-align: center;
			margin-top: 20upx;
			justify-content: center;
		}
		.replacement_box_item {
			display: flex;
			justify-content: space-between;
			border-bottom: 1px solid #ece7e7;
			padding: 20upx 0upx;
			align-items: center;
			&:last-child {
				border-bottom: 0px solid #ece7e7;
			}
			.replacement_box_right {
				color: #666;
				font-size: 26upx;
				display: flex;
				justify-content: flex-end;
				flex: 4;
				input {
					font-size: 26upx;
					width: 80%;
				}
			}
			.checkbox-box{
				display: flex;
				align-items: center;
				width: 30upx;
				height: 30upx;
				.checkbox{
					width: 30upx;
					height: 30upx;
					border-radius: 100%;
					border: solid 1upx #E80505;
					display: flex;
					align-items: center;
					justify-content: center;
					.on{
						width: 24upx;
						height: 24upx;
						border-radius: 100%;
						background-color: #E80505;
						align-self: center;
						justify-content: center;
					}
				}
				.text{
					font-size: 28upx;
					margin-left: 10upx;
				}
			}
		}
	}
</style>
